﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using SES.CMS.BL;
using SES.CMS.DO;

namespace SES.CMS.AdminCP.PageUC
{
    public partial class ucListArticle : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                grvArtDataSource(new cmsArticleBL().SelectAll());
               // Functions.ddlDatabinder(cboCategory, cmsCategoryDO.CATEGORYID_FIELD, cmsCategoryDO.TITLE_FIELD, new cmsCategoryBL().SelectAll());
               // cboCategory.Items.Insert(0, "Chọn tất cả -----------");
                bindToDropDown(cboCategory);
            }
        }
        protected void grvArtDataSource(DataTable dt)
        {
            gvAt.DataSource = dt;
            gvAt.DataBind();
        }
        protected void gvArticle_StartRowEditing(object sender, DevExpress.Web.Data.ASPxStartRowEditingEventArgs e)
        {

        }

        protected void cboCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cboCategory.SelectedIndex <= 0)
            {
                grvArtDataSource(new cmsArticleBL().SelectAll());
            }
            else
            {
                grvArtDataSource(new cmsArticleBL().SelectByCategoryID(int.Parse(cboCategory.SelectedValue)));
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Response.Redirect("Default.aspx?Page=Article");
        }



        protected void gvAt_SelectedIndexChanged(object sender, EventArgs e)
        {
            int ArticleID = int.Parse(gvAt.DataKeys[gvAt.SelectedIndex].Value.ToString());
            Response.Redirect("Default.aspx?Page=Article&ArticleID=" + ArticleID.ToString());
        }

        protected void gvAt_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            new cmsArticleBL().Delete(new cmsArticleDO { ArticleID = Convert.ToInt32(gvAt.DataKeys[e.RowIndex].Value) });
            Functions.Alert("Xóa bản tin thành công!", Request.Url.ToString());
        }

        protected void gvAt_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvAt.PageIndex = e.NewPageIndex;
            if (cboCategory.SelectedIndex <= 0)
            {
                grvArtDataSource(new cmsArticleBL().SelectAll());
            }
            else
            {
                grvArtDataSource(new cmsArticleBL().SelectByCategoryID(int.Parse(cboCategory.SelectedValue)));
            }

        }

        protected void gvAt_PageIndexChanged(object sender, EventArgs e)
        {

        }
        public void bindToDropDown(DropDownList ddl)
        {
            DataTable dtCate = new cmsCategoryBL().SelectAdmin(false); //new DataView(new cmsMenuBL().SelectAll(),"IsContent = 0","",DataViewRowState.CurrentRows).ToTable();
            foreach (DataRow dr in dtCate.Rows)
            {
                if (dr["ParentID"].ToString().Equals("0"))
                {
                    ListItem item = new ListItem();
                    item.Value = dr["CategoryID"].ToString();
                    item.Text = "+ " + dr["Title"].ToString();
                    ddl.Items.Add(item);
                    GetChildItems(dr["CategoryID"].ToString(), dtCate, ddl);
                }
            }
        }
        private void GetChildItems(string parentID, DataTable dtTemp, DropDownList ddl)
        {
            foreach (DataRow dr in dtTemp.Rows)
            {
                if (dr["ParentID"].ToString().Equals(parentID))
                {
                    ListItem listChild = new ListItem();
                    listChild.Text = "-------" + dr["Title"].ToString();
                    listChild.Value = dr["CategoryID"].ToString();
                    listChild.Attributes.Add("style", "background:#f9f9f9;color:#648293;");
                    ddl.Items.Add(listChild);
                    //       GetChildItems(dr["MenuID"].ToString(), dtTemp, ddl);
                }
            }
        }

    }
}